package com.example.demo.config;/**
 * Created by kete on 2017-12-18.
 */

import com.example.demo.config.properties.MqProperties;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.pool.PooledConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.ConnectionFactory;

/**
 * 作者：kete
 * 创建时间：2017-12-18 14:24
 * 功能描述：
 * 版本：
 */
@Configuration
public class MqConfig {

    @Autowired
    MqProperties mqProperties;

    /**
     * mq 连接工厂
     * @return
     */
    @Bean
    @ConditionalOnProperty(prefix = "guns", name = "mq-open", havingValue = "true")
    public ConnectionFactory connectionFactory(){
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL(mqProperties.getBrokerURL());
        connectionFactory.setUserName(mqProperties.getUserName());
        connectionFactory.setPassword(mqProperties.getPassword());
        return connectionFactory;
    }

    /**
     * 连接池
     * @return
     */
    @Bean
    @ConditionalOnProperty(prefix = "guns", name = "mq-open", havingValue = "true")
    public PooledConnectionFactory pooledConnectionFactory(){
        PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory();
        pooledConnectionFactory.setConnectionFactory(connectionFactory());
        pooledConnectionFactory.setMaxConnections(10);
        return pooledConnectionFactory;
    }

    /**
     * 缓存连接
     * @return
     */
    @Bean
    @ConditionalOnProperty(prefix = "guns", name = "mq-open", havingValue = "true")
    public CachingConnectionFactory cachingConnectionFactory(){
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(connectionFactory());
        cachingConnectionFactory.setSessionCacheSize(Integer.parseInt(mqProperties.getSessionCacheSize()));
        return cachingConnectionFactory;
    }

    /**
     * JMS 队列的模板类
     */
    @Bean
    @ConditionalOnProperty(prefix = "guns", name = "mq-open", havingValue = "true")
    public JmsTemplate jmsQueueTemplate(){
        return new JmsTemplate(cachingConnectionFactory());
    }

    /**
     * 订阅模板
     * @return
     */
    @Bean
    @ConditionalOnProperty(prefix = "guns", name = "mq-open", havingValue = "true")
    public JmsTemplate jmsTopicTemplate(){
        JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory());
        jmsTemplate.setPubSubDomain(true);
        return jmsTemplate;
    }

    @Bean
    @ConditionalOnProperty(prefix = "guns", name = "mq-open", havingValue = "true")
    public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(){
        DefaultJmsListenerContainerFactory defaultJmsListenerContainerFactory = new DefaultJmsListenerContainerFactory();
        defaultJmsListenerContainerFactory.setConnectionFactory(cachingConnectionFactory());
        return defaultJmsListenerContainerFactory;
    }


    @Bean
    @ConditionalOnProperty(prefix = "guns", name = "mq-open", havingValue = "true")
    public DefaultJmsListenerContainerFactory jmsTopicListenerContainerFactory(){
        DefaultJmsListenerContainerFactory defaultJmsListenerContainerFactory = new DefaultJmsListenerContainerFactory();
        defaultJmsListenerContainerFactory.setConnectionFactory(cachingConnectionFactory());
        defaultJmsListenerContainerFactory.setPubSubDomain(true);
        return defaultJmsListenerContainerFactory;
    }


}
